﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace KalevaAalto.WorkingFlowPart
{
    public class Workflow
    {
        public Stopwatch _globalStopwatch = new Stopwatch();
        public Stopwatch _stopwatch = new Stopwatch();
        private string _subName = string.Empty;
        private Action<string>? _log;
        private ICollection<Task> _tasks = new List<Task>();
        private string _workingContent = string.Empty;
        

        public void Show(string message)
        {
            if (_log is not null)
            {
                _log(message);
            }
        }

        public Workflow()
        {

        }



        public Workflow(Action<string>? log, string subName)
        {
            _log = log;
            _subName = subName;
            _globalStopwatch.Start();
            Show(@"---------------------------------------------");
        }


        public Workflow(string subName, Action<string>? log)
        {
            _log = log;
            _subName = subName;
            _globalStopwatch.Start();
            Show(@"---------------------------------------------");

        }

        public Workflow(string subName)
        {
            _subName = subName;
            _globalStopwatch.Start();
            Show(@"---------------------------------------------");
        }

        public string SubName { get => _subName;set{ 
                _subName = value;
                _globalStopwatch.Start();
                Show(@"---------------------------------------------"); 
            } }

        public Action<string>? Log
        {
            set => _log = value;
        }



        public string WorkingContent
        {
            get
            {
                return _workingContent;
            }
            set
            {
                if (!string.IsNullOrEmpty(_workingContent))
                {
                    Show($"进程：{_subName}：{_workingContent}成功！！！" + _stopwatch.ClockString());
                }
                _stopwatch.Restart();
                _workingContent = value;
            }
        }
        public void End()
        {
            if (!string.IsNullOrEmpty(_workingContent))
            {
                Show($"进程：{_subName}：{_workingContent}成功！！！" + _stopwatch.ClockString());
            }
            Show(@"==============================================");
            Show($"进程：{_subName}：结束！！！！" + _globalStopwatch.ClockString());
        }
        public void Stop(string message = EmptyString)
        {
            if (string.IsNullOrEmpty(message))
            {
                Show($"进程：{_subName}：{_workingContent}成功！！！" + _stopwatch.ClockString());
            }
            else
            {
                Show($"进程：{_subName}：{_workingContent}成功，{message}！！！" + _stopwatch.ClockString());
            }

            _workingContent = string.Empty;
        }

        public void Error(Exception error)
        {
            Show(@"==============================================");
            Regex regex = new Regex(@"\s+");
            string errorMessage = regex.Replace(error.Source + "：" + error.Message, " ");
            Show($"进程：{_subName}：{(string.IsNullOrEmpty(_workingContent) ? _subName : _workingContent)}：异常：{errorMessage}");
        }

        public void Error(string errorMessage)
        {
            Show(@"==============================================");
            Show($"进程：{_subName}：{(string.IsNullOrEmpty(_workingContent) ? _subName : _workingContent)}：异常：{errorMessage}");
        }

    }
}
